import axios from '~plugins/axios'
import * as types from '../../mutation-types/user_login/index';
import { setToken } from '~/utils/auth'
import router from '../../../.nuxt/router'

/**
 * 用户登录
 *
 * @param commit
 * @param state
 * @param preload
 */
export const getUserLogin = ({commit, state, dispatch}, preload) => {
    return axios.get(process.env.baseUrl + '/login', {
        params: preload
    })

};

/**
 * 获取用户个人信息
 *
 * @param commit
 * @param state
 * @param preload
 */
export const getUserPersonalData = ({commit}, preload) => {
    return axios.get(process.env.baseUrl + '/member/get', {
        params: preload || {} //必须加上这句,不然token加不上去
    })
};
/**
 * 用户退出登录
 *
 * @param commit
 * @param state
 * @param preload
 */
export const getLoginOut = ({commit}) => {
    return axios.post(process.env.baseUrl + '/logout', {}) //必须加上{},不然token加不上去
        .then((response) => {
            if (!response) {
                return
            }
            const success = response.data.successed;
            const code = response.data.code;
            if (success) {
                setToken('token', '');
                setToken('userData', '');
                commit(types.USER_LOGIN, {
                    isLogin: false
                });
                commit(types.USER_TOKEN, {
                    userToken: ''
                });
                router.go({
                    path: '/login',
                    force: true
                });
            }
        })
};


/**
 * 获取所管理的用户接口
 *
 * @param commit
 * @param state
 * @param preload
 */
export const getUserOwnerData = ({commit}, preload) => {
    return axios.get(process.env.baseUrl + '/member/owner', {
        params: preload
    })
};